package com.wss.lsl.lucene.demo;

import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

public class LuceneIn5Minutes {
	
	public static void main(String[] args) throws IOException, ParseException {
		
		// index
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
		Directory directory = new RAMDirectory();
		
		IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_44, analyzer);
		IndexWriter indexWriter = new IndexWriter(directory, writerConfig);
		addDoc(indexWriter, "Lucene in Action", "193398817");
		addDoc(indexWriter, "Lucene for Dummies", "55320055Z");
		addDoc(indexWriter, "Managing Gigabytes", "55063554A");
		addDoc(indexWriter, "The Art of Computer Science", "9900333X");
		indexWriter.close();
		
		// query
		Query query = new QueryParser(Version.LUCENE_44, "title", analyzer).parse("lucene");
		
		// search
		int hitPerPage = 10;
		IndexReader indexReader = DirectoryReader.open(directory);
		IndexSearcher indexSearcher = new IndexSearcher(indexReader);
		TopScoreDocCollector topScoreDocCollector = TopScoreDocCollector.create(hitPerPage, true);
		indexSearcher.search(query, topScoreDocCollector);
		ScoreDoc[] hitScores = topScoreDocCollector.topDocs().scoreDocs;
		
		// display
		System.out.println("Found "+ hitScores.length + " hits.");
		for(ScoreDoc hit : hitScores){
			Document doc = indexSearcher.doc(hit.doc);
			System.out.println("title: "+doc.get("title")+"\tisbn:"+ doc.get("isbn"));
		}
		
		// boolean query
		BooleanQuery bq = new BooleanQuery();
		bq.add(query, BooleanClause.Occur.MUST);
		bq.add(new TermQuery(new Term("isbn", "55320055Z")), BooleanClause.Occur.MUST_NOT);
		hitScores = indexSearcher.search(bq, 100).scoreDocs;
		
		// display
		System.out.println();
		System.out.println("**************************** boolean query ****************************");
		System.out.println("Found "+ hitScores.length + " hits.");
		for(ScoreDoc hit : hitScores){
			Document doc = indexSearcher.doc(hit.doc);
			System.out.println("title: "+doc.get("title")+"\tisbn:"+ doc.get("isbn"));
		}
		indexReader.close();
	}
	
	private static void addDoc(IndexWriter indexWriter, String title, String isbn) throws IOException{
		Document doc = new Document();
		doc.add(new TextField("title", title, Field.Store.YES));
		doc.add(new StringField("isbn", isbn, Field.Store.YES));
		indexWriter.addDocument(doc);
	}
}
